$(function () {
    function Content() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');

        // 初始化
        this.init();
    }

    Content.prototype.Constructor = Content;

    Content.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    Content.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource": "/content/page",
            "aoColumns": [
                {"sWidth": "20%", "sTitle": "标题", "sClass": "center", "mData": "title", "bSortable": false},
                {"sWidth": "15%", "sTitle": "署名", "sClass": "center", "mData": "userName", "bSortable": false},
                {"sWidth": "15%", "sTitle": "状态", "sClass": "center", "mData": "state", "bSortable": false},
                {"sWidth": "15%", "sTitle": "创建时间", "sClass": "center", "mData": "created", "bSortable": false},
                {"sWidth": "15%", "sTitle": "发布时间", "sClass": "center", "mData": "published", "bSortable": false},
                {"sWidth": "20%", "sTitle": "操作", "sClass": "center", "mData": "id", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];

                var state = aData['state'];
                if (state == 1) {
                    $('td:eq(2)', nRow).html('<span class="label label-success arrowed-in">已发布</span>');
                } else if (state == 0) {
                    $('td:eq(2)', nRow).html('<span class="label label-info arrowed-in">编辑中</span>');
                }

                if (aData['created']) {
                    $('td:eq(3)', nRow).html(SysTool.Date.formatDate(aData['created'], 'yyyy-MM-dd hh:mm:ss'));
                }
                if (aData['published']) {
                    $('td:eq(4)', nRow).html(SysTool.Date.formatDate(aData['published'], 'yyyy-MM-dd hh:mm:ss'));
                }
                //操作
                var option = '';
                if (state === 1) {
                    option += '<a title="撤回发布" contentId="' + id + '" href="javascript:void(0)" class="btn btn-xs btn-warning">撤回</a>&nbsp;';
                } else {
                    option += '<a title="发布内容" contentId="' + id + '" href="javascript:void(0)" class="btn btn-xs btn-success">发布</a>&nbsp;';
                    option += '<a title="编辑内容" href="' + '/content/edit/' + id + '" class="btn btn-xs btn-info">编辑</a>&nbsp;';
                }
                option += '<a title="删除内容" contentId="' + id + '" href="javascript:void(0)" class="btn btn-xs btn-danger">删除</a>';

                $('td:eq(5)', nRow).html(option);
            },
            "fnServerParams": function (aoData) {
                queryCondition("#search-form", aoData);
            },
            "fnStateLoaded": function (oSettings, oData) {
//                alert(JSON.stringify(oData));
            }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
        }
    };

    Content.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    Content.prototype.registEvent = function () {
        var that = this;
        that.dataTable.on('click', 'a.btn-danger', function () {
            var $that = $(this);
            BootBox.confirm('确定删除当前内容？', function (b) {
                if (!b) return;
                var ids = $that.attr('contentId');
                SysTool.Ajax.ajaxJsonCall('content/delete/' + ids, {}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });
        that.dataTable.on('click', 'a.btn-warning', function () {
            var $that = $(this);
            BootBox.confirm('内容撤回后，可以再次编辑后发布，继续？', function (b) {
                if (!b) return;
                var ids = $that.attr('contentId');
                SysTool.Ajax.ajaxJsonCall('content/cancelPub/' + ids, {}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });
        that.dataTable.on('click', 'a.btn-success', function () {
            var $that = $(this);
            BootBox.confirm('确定发布当前内容？', function (b) {
                if (!b) return;
                var ids = $that.attr('contentId');
                SysTool.Ajax.ajaxJsonCall('content/publish/' + ids, {}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.$btn_search_clear.click(function () {
            $('#title').val('');
            $('#createStartTime').val('');
            $('#createEndTime').val('');
            that.$seachr_btn.click();
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.Content = new Content();
});